<?php
App::uses('Component', 'Controller');
App::import('Vendor', 'emailClass', array('file' => 'PHPMailer/class.phpmailer.php'));

/**
 * Emailer Email Component
 */
class EmailerComponent extends Component
{

    function send($controller, $email_address, $dataObj, $title, $params = NULL)
    {

        $renderData = $dataObj['data'];

        $controller->set(compact('renderData'));
        $rendered_mesg = $controller->render($dataObj['render_element']);

        echo $rendered_mesg;

        $mail = new PHPMailer();
        $body = $rendered_mesg;
        $body = eregi_replace("[\]", '', $body);

        $mail->IsSMTP(); // telling the class to use SMTP
        $mail->Host = "ssl://smtp.gmail.com"; // SMTP server
        $mail->SMTPDebug = 2; // enables SMTP debug information (for testing)
        // 1 = errors and messages
        // 2 = messages only
        $mail->SMTPAuth = true; // enable SMTP authentication
        $mail->Host = "ssl://smtp.gmail.com"; // sets the SMTP server
        $mail->Port = 465; // set the SMTP port for the GMAIL server
        $mail->Username = "interpaygh@gmail.com"; // SMTP account username
        $mail->Password = "interpay_gh"; // SMTP account password
        //$mail->SMTPSecure = 'tls';

        // interpayghnoreply@gmail.com
        $mail->SetFrom('noreply@interpayafrica.com', 'Interpay Service');
        $mail->Subject = $title;
        $mail->MsgHTML($body);

        $address = $email_address;
        $mail->AddAddress($address, $renderData['fname'] . ' ' . $renderData['lname']);
        $mail->AddEmbeddedImage(WWW_ROOT . '/img/logo_sm.png', 'logo_2u');
        //$mail->AddAttachment("images/phpmailer.gif");      // attachment
        //$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment

        $result = $mail->Send();
        //echo "Mailer Error: " . $mail->ErrorInfo;
        // exit;

        if ($result) {
            return true;
        } else {
            return false;
        }
    }
}

?>